package com.qianfeng.smartdevice.service.impl;

import com.qianfeng.smartdevice.dto.HumitureEchartsDto;
import com.qianfeng.smartdevice.mapper.SysHumitureMapper;
import com.qianfeng.smartdevice.pojo.SysHumiture;
import com.qianfeng.smartdevice.service.HumitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wenka on 2022/3/28 22:14
 *
 * @author wenkaixin
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class HumitureServiceImpl implements HumitureService {
    private SysHumitureMapper sysHumitureMapper;

    @Autowired
    public void setSysHumitureMapper(SysHumitureMapper sysHumitureMapper) {
        this.sysHumitureMapper = sysHumitureMapper;
    }

    @Override
    public HumitureEchartsDto findOneHourDate() {
        //如何找到一小时之前的时间
        //当前时间
        Instant now = Instant.now();
        //以当前时间为准向前推3600秒
        Instant instant = now.plusSeconds(-3600);
        List<SysHumiture> humitureList = sysHumitureMapper.findDataByTime(Date.from(instant), Date.from(now));
        List<Date> times = humitureList.stream().map(SysHumiture::getUploadTime).collect(Collectors.toList());
        List<Double> temperatureList = humitureList.stream().map(SysHumiture::getTemperature).collect(Collectors.toList());
        List<Double> humidityList = humitureList.stream().map(SysHumiture::getHumidity).collect(Collectors.toList());
        HumitureEchartsDto humitureEchartsDto = new HumitureEchartsDto();
        humitureEchartsDto.setHumidityList(humidityList);
        humitureEchartsDto.setTemperatureList(temperatureList);
        humitureEchartsDto.setTimes(times);
        return humitureEchartsDto;
    }
}
